//# sourceURL=http://uMapApp/Module/HelpModule
define([
    "mhjs/mhjs",
    "uMap/uMap",
    "uEarth/uEarth",
    "./Module"
    , "../UI/Panel/FloatPanel"
    , "../UI/Panel/PanelBlock/TreeBlock"
    , "../UI/Tree/Tree"
], function (
    mhjs,
    uMap,
    uEarth,
    Module
    , FloatPanel
    , TreeBlock
    , Tree
) {

    function HelpModule(option) {

        var _this = this;

        Module.call(_this, option);

        _this.title = "帮助文档";
        _this.icon = _this.app.icons.help;

        _this.panels = [];

        _this.rightPanel = new FloatPanel({
            "class": "rightPanel newPanel SettingsModule",
            "title": "帮助文档"
        });

        _this.ruleBlock = new TreeBlock({
            "title": "帮助文档",
            "class": "fullBlock"
        });

        _this.rightPanel
            .append(_this.ruleBlock);

        _this.app.ui.layout.right.append(_this.rightPanel);

        _this.getFile(_this.title, function (list) {

            if (list.length > 0) {
                _this.renderFiles(_this.ruleBlock.tree, list);
                _this.ruleBlock.tree.openChild();
            } else {
                _this.ruleBlock.tree.hide();
            }

        });
    }

    var _class = HelpModule;

    mhjs.Inherit(_class, Module);

    Object.defineProperties(_class.prototype, {
        "open"       : {
            value: function () {
                var _this = this;
                _this.rightPanel.open();
                _this.isOpened = true;
            }
        },
        "close"      : {
            value: function () {
                var _this = this;
                _this.rightPanel.close();
                _this.isOpened = false;
            }
        },
        "toggle"     : {
            value: function () {
                var _this = this;
                if (_this.isOpened) {
                    _this.close();
                } else {
                    _this.open();
                }
            }
        },
        "getFile"    : {
            value: function (path, callback) {
                var _this = this;

                _this.app.servers.DC.requests.sc_files.send({"path": path}, function (json) {
                    if (json.data && json.data.list) {
                        callback && callback(json.data.list);
                    }
                });

            }
        },
        "renderFiles": {
            value: function (parentTree, fileList) {
                var _this = this;

                $.each(fileList, function (i, file) {

                    var tree = new Tree({
                        "title": file.fileName
                    });

                    if (file.file) {

                        if (file.url) {
                            tree.url = file.url;
                        }

                    } else {

                        tree.list = [];
                        tree.isChildLoaded = false;

                        tree.ui.titleDiv.on("click", function () {
                            if (!tree.isChildLoaded) {
                                _this.getFile(file.path, function (list) {
                                    _this.renderFiles(tree, list);
                                    tree.openChild();
                                });
                            }
                        });

                    }

                    parentTree.add(tree);


                });


            }
        }
    });

    return _class;

});